package com.yfp.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.device.domain.OrderDetailWait;
import com.yfp.device.domain.dto.OrderDetailWaitDTO;
import com.yfp.device.mapper.OrderDetailWaitMapper;
import com.yfp.device.service.OrderDetailWaitService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【order_detail_wait(订单预计排队时间)】的数据库操作Service实现
 * @createDate 2024-09-03 11:52:23
 */
@Slf4j
@Service
public class OrderDetailWaitServiceImpl extends ServiceImpl<OrderDetailWaitMapper, OrderDetailWait> implements OrderDetailWaitService {

    @Resource
    private OrderDetailWaitMapper orderDetailWaitMapper;

    @Override
    public void saveQueueTime(List<OrderDetailWaitDTO> orderDetailWaitDTO) {
        log.info("新增订单预计排队时间,orderDetailWaitDTO:{}", orderDetailWaitDTO);
        if (CollUtil.isEmpty(orderDetailWaitDTO)) {
            return;
        }
        OrderDetailWaitDTO first = CollUtil.getFirst(orderDetailWaitDTO);
        List<String> orderDetailIds = orderDetailWaitDTO.stream().map(OrderDetailWaitDTO::getOrderDetailId).collect(Collectors.toList());
        Set<String> orderDetailIdSet = orderDetailWaitMapper.selectByMachineIdAndOrderDetailId(first.getMachineId(), orderDetailIds).stream().map(OrderDetailWait::getOrderDetailId).collect(Collectors.toSet());
        // 数据库中存在的记录则更新，不存在的插入
        DateTime date = DateUtil.date();

        // 待更新的记录
        List<OrderDetailWait> updateList = orderDetailWaitDTO.stream().filter(item -> CollUtil.contains(orderDetailIdSet, item.getOrderDetailId())).map(item -> {
            OrderDetailWait bean = BeanUtil.toBean(item, OrderDetailWait.class);
            bean.setUpdateTime(date);
            return bean;
        }).collect(Collectors.toList());
        // 待插入的记录
        List<OrderDetailWait> insertList = orderDetailWaitDTO.stream().filter(item -> !CollUtil.contains(orderDetailIdSet, item.getOrderDetailId())).map(item -> {
            OrderDetailWait bean = BeanUtil.toBean(item, OrderDetailWait.class);
            bean.setUpdateTime(date);
            bean.setCreateTime(date);
            return bean;
        }).collect(Collectors.toList());

        this.updateByMachineIdAndOrderDetailId(updateList);
        this.insertBatch(insertList);
    }

    @Override
    public void updateByMachineIdAndOrderDetailId(List<OrderDetailWait> updateList) {
        updateList.forEach(item -> {
            orderDetailWaitMapper.updateByMachineIdAndOrderDetailId(item);
        });
    }

    @Override
    public void insertBatch(List<OrderDetailWait> insertList) {
        if (CollUtil.isEmpty(insertList)) {
            return;
        }
        orderDetailWaitMapper.insertBatch(insertList);
    }

    @Override
    public OrderDetailWait findByOrderDetailId(String orderDetailId) {
        return orderDetailWaitMapper.findByOrderDetailId(orderDetailId);
    }

    @Override
    public void updateStatusByOrderDetailId(String orderDetailId, Integer status) {
        orderDetailWaitMapper.updateStatusByOrderDetailId(orderDetailId, status);
    }
}




